package cn.tedu.zx.ams.adapter.controller;


import cn.tedu.zx.ams.service.RoleService;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.pojo.admin.parm.RoleAddNewParam;
import cn.tedu.zx.pojo.admin.parm.RoleUpdateParam;
import cn.tedu.zx.pojo.admin.vo.RoleListItemVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "02. 角色管理员模块")
@Slf4j
@Validated
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/addNew")
    @ApiOperation("添加角色数据")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/role/insert')")
    public JsonResult<Void> addNew(@Valid RoleAddNewParam roleAddNewParam){
        roleService.addNew(roleAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAnyAuthority('/role/read')")
//    @PreAuthorize("hasAuthority('/ams/admin/read')")//只有该权限的才可以访问
    public JsonResult<List<RoleListItemVO>> list() {
        log.debug("开始处理【查询角色列表】的业务");
        List<RoleListItemVO> list = roleService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/update")
    @ApiOperation("修改角色数据")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/role/update')")
    public JsonResult<Void> update(@Valid RoleUpdateParam roleUpdateParam){
        log.debug("开始处理【修改角色数据】的业务");
        roleService.update(roleUpdateParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除角色数据")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/role/delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "提交的角色ID必须大于0", required = true, dataType = "Long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "请提交合法的ID值") @RequestParam Long id) {
        log.debug("开始处理【删除角色数据】的业务");
        roleService.delete(id);
        return JsonResult.ok();
    }
}
